package org.example.lotterysystem.service.activitystatus.operater;

import jakarta.annotation.Resource;
import org.example.lotterysystem.common.domain.dto.ConvertActivityStatusDTO;
import org.example.lotterysystem.common.domain.enums.ActivityPrizeStatusEnum;
import org.example.lotterysystem.mapper.ActivityPrizeMapper;
import org.example.lotterysystem.mapper.dataobject.ActivityPrizeDO;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator{

    @Resource
    ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        if(activityId==null || prizeId==null || targetPrizeStatus==null){
            return false;
        }
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectByAPId(activityId, prizeId);
        if(activityPrizeDO==null){
            return false;
        }
        // 判断当前奖品状态和目标状态是否一致
        if (targetPrizeStatus.name().equalsIgnoreCase(activityPrizeDO.getStatus())) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        try {
            activityPrizeMapper.updateStatus(activityId, prizeId, targetPrizeStatus.name());
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
